package com.qf.om.myutils;

import com.qf.om.entity.Admin;
import com.qf.om.entity.Student;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * static 修饰的成员 是类级别的成员，全局只有一份
 * 非static 的成员 是对象级别的成员，每个对象都有一份
 * <p>
 * description: 数据的存储工具类 - 暂时充当数据库的作用
 */
public class DataUtils {
    private static List<Admin> adminList = new ArrayList<Admin>();
    private static List<Student> studentList = new ArrayList<Student>();

    static {
        Admin admin = new Admin();
        admin.setUsername("admin");
        admin.setPassword("123");
        admin.setName("小明");
        admin.setHometown("广东省");
        admin.setSex("男");
        admin.setBirthday("2000-1-1");
        admin.setPowers(Arrays.asList("1","2","3"));
        addAmin(admin);

        Student student1 =new Student();
        student1.setName("张三");
        student1.setAge(18);
        student1.setSex(1);
        addStudent(student1);

        Student student2 =new Student();
        student2.setName("李四");
        student2.setAge(20);
        student2.setSex(1);
        addStudent(student2);

        Student student3 =new Student();
        student3.setName("小美");
        student3.setAge(21);
        student3.setSex(2);
        addStudent(student3);
    }

    public static void addAmin(Admin admin) {
        adminList.add(admin);
    }

    public static void addStudent(Student student) {
        studentList.add(student);
    }

    //    查询方法
    public static Admin queryAdmin(String username, String password) {
//            遍历集合
        for (Admin admin : adminList) {
            if (admin.getUsername().equals(username) && admin.getPassword().equals(password)) {
                return admin;
            }
        }
//        应该返回一个状态
        return null;
    }


    public static List<Student> queryStudent() {
//       复制一份集合数据提供浏览，防止其他人篡改集合中的数据
        return new ArrayList<>(studentList);
    }

    public static boolean deleteStudent(String name){
        List<Student> newList=new ArrayList<>(studentList);
        for (Student student:newList) {
            if (student.getName().equals(name)){
                studentList.remove(student);
                return true;
            }
        }
        return false;
    }
}
